package com.example.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.eduservice.entity.EduCourse;
import com.example.eduservice.entity.vo.CourseInfoVo;
import com.example.eduservice.entity.vo.CourseListVo;
import com.example.eduservice.entity.vo.CoursePublishVo;
import com.example.eduservice.service.EduCourseService;
import com.example.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xiuxian
 * @since 2022-06-26
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService  courseService;

    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String id=courseService.saveCourseInfo(courseInfoVo);
        return  R.ok().data("courseId",id);

    }

    //根据课程id查询课程基本信息
    @GetMapping("/getCourseInfo/{courseId}")
    public  R   getCourseInfo(@PathVariable String courseId){
            CourseInfoVo courseInfoVo=courseService.getCourseInfo(courseId);
            return  R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("/updateCourseInfo")
    public  R  updateCourse(@RequestBody CourseInfoVo courseInfoVo){
          courseService.updateCoureInfo(courseInfoVo);
          return  R.ok();
    }


    //根据课程id查询课程确认信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public  R getPublishCourseInfo(@PathVariable String id){
         CoursePublishVo coursePublishVo=courseService.publishCourseInfo(id);
            return  R.ok().data("publishCourse",coursePublishVo);
    }

    //修改课程状态
    //课程最终发布
    @PostMapping("/publishCourse/{id}")
    public   R publishCourse(@PathVariable String id){
        EduCourse eduCourse=new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus(EduCourse.COURSE_NORMAL);  //设置课程发布状态
        courseService.updateById(eduCourse);

        return  R.ok();

    }

    //课程列表
    @GetMapping("/getCourseList")
    public  R  getCourseList(){
        List<EduCourse> eduCourses = courseService.list(null);
        return  R.ok().data("list",eduCourses);
    }

    //删除课程
    @DeleteMapping("/{courseId}")
    public  R  deleteCourse(@PathVariable String courseId){

        courseService.removeCourse(courseId);
        return  R.ok();
    }

    //条件查询带分页
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public  R  pageCourseConditon(@PathVariable long current, @PathVariable long limit,
            @RequestBody(required = false)CourseListVo courseListVo) {
        //创建page对象
        Page<EduCourse> pagecourse=new Page<>(current,limit);

        //构造条件
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();

        //判断条件是否为空
        String title=courseListVo.getTitle();
        String status=courseListVo.getStatus();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.like("status",status);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        courseService.page(pagecourse,wrapper);
        long total= pagecourse.getTotal();   //总记录数
        List<EduCourse> records = pagecourse.getRecords(); //数据list集合

        return  R.ok().data("total",total).data("rows",records);


    }


}

